package net.caixiaomi.info.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 2018/9/7.
 */

public class HornorSelectView extends LinearLayout{

    private Context mContext;

    private LinearLayout llt_title;
    private RelativeLayout match_before_group,
            match_after_group
                    ;

    private View cursor;

    private int currentIndex;//当前页卡编号
    public int subTitleWidth;//子标题宽度，cursor平移距离
    private int mLineWidth; //每个条目的宽度
    private int mSpaceWidth;    //每个条目文字的间隙

    private int mScreenWidth;

    private MatchingSelectCallBack mCallBack;

    private List<String> mData;

    private List<TextView> mTvList;

    public HornorSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        mTvList=new ArrayList<>();
        mScreenWidth= CommonApp.mScreenWidth;
        View view=View.inflate(context, R.layout.hornor_select_group_layout,null);
        addView(view);

        llt_title = (LinearLayout) findViewById(R.id.llt_title);

        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) llt_title.getLayoutParams();
        params.width = mScreenWidth;
        llt_title.setLayoutParams(params);

        cursor = findViewById(R.id.cursor);

    }

    private void initData(){
        for(int i=0;i<mData.size();i++){
            View view=View.inflate(mContext,R.layout.hornor_select_item_layout,null);
            LayoutParams params=new LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT,1);
            view.setLayoutParams(params);
            llt_title.addView(view);
            TextView tv=view.findViewById(R.id.match_after_tv);
            tv.setText(mData.get(i));
            mTvList.add(tv);
            RelativeLayout ll=view.findViewById(R.id.match_after_group);
            final int position=i;
            ll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCallBack.matchingBarSelected(position);
                }
            });
        }
    }

    public void setCallBack(MatchingSelectCallBack callBack,List<String> data){
        mCallBack=callBack;
        mData=data;
        subTitleWidth=mScreenWidth/data.size();
        post(new Runnable() {
            @Override
            public void run() {
                initTabLine();
                setPageSelected(0);
            }
        });
        initData();
    }

    public void setPageSelected(int position){
        Animation animation = new TranslateAnimation(
                currentIndex * subTitleWidth+mSpaceWidth,  //from X
                position * subTitleWidth+mSpaceWidth,      //to X
                0, 0
        );

        //cursor滑动
        currentIndex = position;
        animation.setFillAfter(true);//动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setInterpolator(new DecelerateInterpolator());//减速动画
        animation.setDuration(300);//动画持续时间0.3秒
        cursor.startAnimation(animation);
        llt_title.getChildAt(position).requestFocus();

        //title的text变色
        setMatchingSelect(position);
    }

    /**
     * 初始化tab下划线
     * 实现步骤：
     * 1、获取屏幕大小 比例设置宽度
     * 2、然后有几个tab就分几份  移动时改变外边距参数
     */
    private void initTabLine() {
        mLineWidth = mScreenWidth / mTvList.size();
        mSpaceWidth=(mLineWidth-mTvList.get(0).getMeasuredWidth())/2;

        RelativeLayout.LayoutParams mLineparams = (RelativeLayout.LayoutParams) cursor.getLayoutParams();
        mLineparams.width = mTvList.get(0).getMeasuredWidth();
        cursor.setLayoutParams(mLineparams);
    }


    /**
     * 选择比赛栏目
     */
    private void setMatchingSelect(int type){
        clearMatchingSelect();
        mTvList.get(type).setTextColor(getResources().getColor(R.color.orange_primary));
    }

    private void clearMatchingSelect(){
        for(int i=0;i<mTvList.size();i++){
            mTvList.get(i).setTextColor(getResources().getColor(R.color.primary_dark_text));
        }
    }
}
